﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.Log;

namespace Infrastructure.Extension
{
    public static class StringIntConvert
    {
        #region 将16进制值转换为10进制的值
        /// <summary>
        /// 将16进制值转换为10进制的值
        /// </summary>
        /// <param name="strHex">参数为16进制的值</param>
        /// <returns></returns>
        public static int HexString2Int(this string strHex)
        {
            int result;
            if (int.TryParse(strHex, System.Globalization.NumberStyles.AllowHexSpecifier,null, out result))
            {
                return result;
            }
            else
            {
                CallLogger.LogMessage(typeof(StringIntConvert),string.Format("Can't convert {0} to hex int",strHex));
                return 0;
            }
        }
        #endregion 将16进制值转换为10进制的值


        /// <summary>
        /// 对字符串进行左补位，
        /// </summary>
        /// <param name="length">补位后新字符串的长度</param>
        /// <param name="padChar">用于补位的字符</param>
        /// <returns></returns>
        public static string StringPadLeft(this string str, int length, char padChar)
        {
            if (length <= str.Length)
            {
                return str;
            }
            else
            {
                return str.PadLeft(length - str.Length, padChar);
            }
        }

        public static byte[] HexStringToByteArray(this string hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars-1; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            string debugStr = string.Empty;
            bytes.ToList().ForEach(b => debugStr = string.Concat(debugStr,b.ToString()+"-"));
            CallLogger.LogMessage(typeof(StringIntConvert), string.Format("Output Binary {0}", debugStr));
            return bytes;
        }
    }


}